/*
 * Famon (http://code.google.com/p/famon/)
 * Copyright (c) 2010 Carl Crous.
 *
 * This file is part of Famon.
 *
 * Famon is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * Famon is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public License
 * along with Famon.  If not, see <http://www.gnu.org/licenses/>.
 */

package za.org.foobar.famon.frontend.server.data;

import com.google.appengine.api.datastore.Key;
import com.google.appengine.api.datastore.KeyFactory;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.gwtportlets.portlet.smartgwt.client.CriteriaDto;
import org.gwtportlets.portlet.smartgwt.client.DataTransferObject;
import org.gwtportlets.portlet.smartgwt.client.SmartPortletFactory;
import org.gwtportlets.portlet.smartgwt.server.DataProviderObject;
import org.gwtportlets.portlet.smartgwt.server.SmartFilter;
import za.org.foobar.famon.frontend.client.FamonException;
import za.org.foobar.famon.frontend.client.dto.ApplicationInstanceDto;
import za.org.foobar.famon.frontend.server.ApplicationLogic;
import za.org.foobar.famon.model.ApplicationInstance;

import java.util.List;

/**
 *
 */
public class ApplicationInstanceDpo implements DataProviderObject {
    private final Log log = LogFactory.getLog(getClass());
    public String getDataSourceId() {
        return ApplicationInstanceDto.DATA_SOURCE_ID;
    }

    public List<DataTransferObject> fetchData(SmartPortletFactory f) {
        CriteriaDto criteria = f.getCriteria();
        if (criteria == null) {
            throw new FamonException("No criteria provided in ApplicationInstance Fetch");
        }
        log.info(criteria.toString());
        ApplicationLogic logic = ApplicationLogic.get();
        String keyString = SmartFilter.getSimpleCriteriaValue(criteria, "key");
        Key key = KeyFactory.stringToKey(keyString);

        if (key == null) {
            throw new FamonException("Key not found");
        }
        ApplicationInstanceDto dto = logic.getApplicationInstanceDto(key);
        log.info("Returning: " + dto.name);
        return f.singleResultList(dto);
    }

    public DataTransferObject updateData(SmartPortletFactory f) {
        log.info("Updating App");
        ApplicationInstanceDto dto = (ApplicationInstanceDto)f.getSingleDto();
        ApplicationLogic logic = ApplicationLogic.get();
        ApplicationInstance instance = logic.updateApplicationInstance(dto);
        if (instance == null) {
            throw new FamonException("Could not find instance to update");
        }
        return logic.createApplicationInstanceDto(instance);
    }

    public DataTransferObject addData(SmartPortletFactory f) {
        return null;
    }

    public DataTransferObject removeData(SmartPortletFactory f) {
        return null;
    }
}
